<?php
if (! defined ( 'BASEPATH' ))
	exit ( 'No direct script access allowed' );
class Posts extends MX_Controller {

	var $_table = 'posts';

	function __construct() {
		parent::__construct ();

		$_SESSION ['KCFINDER'] = array ();
		$_SESSION ['KCFINDER'] ['disabled'] = false;

		$this->load->library ( 'ckeditor', array ('instanceName' => 'CKEDITOR1', 'basePath' => base_url () . "ckeditor/", 'outPut' => true ) );
		$this->load->model ( 'posts_model' );
	}

	function detail() {
		$id = ($this->uri->segment ( 4 ) == '') ? 0 : $this->uri->segment ( 4 );
		$detail = $this->posts_model->getDetail ( $id );
		$data ['detail'] = $detail;
		$data ['content'] = 'admin/detail';
		$this->load->view ( 'back_end/index', $data );
	}
	function edit() {
		$this->load->model ( '../../categories/models/categories_model' );
		$id = ($this->uri->segment ( 4 ) == '') ? 0 : $this->uri->segment ( 4 );
		if ($this->input->post ()) {
			$button_edit = $this->input->post ( 'button_edit' );
			$_id = $this->input->post ( 'id' );
			$data = array ();
			$data ['subject'] = ( string ) $this->input->post ( 'subject' );
			$data ['post_content'] = ( string ) $this->input->post ( 'post_content' );
			$data ['post_content_en'] = ( string ) $this->input->post ( 'post_content_en' );
			$data ['subject_en'] = ( string ) $this->input->post ( 'subject_en' );
			$data ['catid'] = ( int ) $this->input->post ( 'catid' );
			$this->posts_model->saveItem ( 'posts', array ('field' => 'id', 'id' => $_id ), $data );
			redirect ( 'admin/posts/listposts' );
		}
		$cats=$this->categories_model->getCategoriesList();
		$data ['cats'] =$cats;
		$detail = $this->posts_model->getDetail ( $id );
		$data ['detail'] = $detail;
		$data ['action'] = 'edit';
		$data ['content'] = 'admin/add';
		$this->load->view ( 'back_end/index', $data );

	}

	function add() {
		$this->load->model ( '../../categories/models/categories_model' );
		if ($this->input->post ()) {
			$data = array ();
			$data ['subject'] = ( string ) $this->input->post ( 'subject' );
			$data ['post_content'] = ( string ) $this->input->post ( 'post_content' );
			$data ['post_content_en'] = ( string ) $this->input->post ( 'post_content_en' );
			$data ['subject_en'] = ( string ) $this->input->post ( 'subject_en' );
			$data ['catid'] = ( int ) $this->input->post ( 'catid' );
			$this->posts_model->saveItem ( 'posts', array ('field' => 'id', 'id' => 0 ), $data );
			redirect ( 'admin/posts/listposts' );
		}
		$cats=$this->categories_model->getCategoriesList();

		$data ['cats'] =$cats;
		$data ['action'] = 'add';
		$data ['content'] = 'admin/add';
		$this->load->view ( 'back_end/index', $data );

	}

	function listposts() {
		$posts = $this->posts_model->getList ();
		$data ['list'] = $posts;
		$data ['content'] = 'admin/list';
		$this->load->view ( 'back_end/index', $data );
	}

	function del($id = NULL) {
		if ($id == NULL) {
			$id = ( int ) $this->uri->segment ( 4 );
		}
		$this->posts_model->del ( $id );
		redirect ( 'admin/posts/listposts' );
	}

	function them() {
	  echo "them bai viet";
	  $data ['content'] = 'admin/them';
	  $this->load->view ( 'back_end/index', $data );

	}
}